package com.example.print.fragment;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.print.R;
import com.example.print.activity.MainActivity;
import com.panlin.printer.utils.CodeUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class PrintTemplateFragment extends Fragment {
    private String TAG = "PrintTemplateActivity";

    private EditText template_Paper_distance, template_chroma, text_label,text_labe2;
    private ImageView img_codes1;
    private Button btn_templateprint,btn_retteprint;

    private MainActivity mContext;
    //private String barcodeData = "abcdefg1234567890ABCDEFGabcdefg1234567890ABCDEFGabcdefg1234567890ABCDEFGabcdefg1234567890ABCDEFG";
    private String barcodeData = "https://jxln-ips-mp.parking.4hiitech.com/s/p/common?payBack|vpyh18788528#%E8%B5%A3B79311";
    private String labelbarcodeData = "123456789ABCD";
    //private boolean print = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_printtemplate, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mContext = (MainActivity) getActivity();

        //走纸距离以及浓度
        text_label = mContext.findViewById(R.id.text_label);
        text_labe2 = mContext.findViewById(R.id.text_label2);
        img_codes1 = mContext.findViewById(R.id.img_codes1);
        template_Paper_distance = mContext.findViewById(R.id.template_Paper_distance);
        template_chroma = mContext.findViewById(R.id.template_chroma);
        btn_templateprint = mContext.findViewById(R.id.btn_templateprint);
        btn_retteprint = mContext.findViewById(R.id.btn_retteprint);
        Log.d(TAG, "blacklabel_status = " + mContext.mPrinter.blacklabel_status);

        if(mContext.mPrinter.blacklabel_status)
        {
            //这里是标签样板测试
            StringBuilder labelBuff = new StringBuilder();
            labelBuff.append("  这是一行测试标签  \r\n");
            img_codes1.setImageBitmap(CodeUtils.generateBitmap(labelbarcodeData, 320, 320));
            text_label.setText(labelBuff.toString());

        }
        else { //这里是热敏样板测试
            StringBuilder textBuff = new StringBuilder();
            img_codes1.setImageBitmap(CodeUtils.generateBitmap(barcodeData, 320, 320));
            textBuff.append("  停车欠费告知单  \r\n");
            textBuff.append("               \r\n");
            textBuff.append("  路段：龙南新世界路 \r\n");
            textBuff.append("  车位号：034034\r\n");
            textBuff.append("  应付金额：￥2.00\r\n");
            textBuff.append("  停车时长：21分钟\r\n");
            textBuff.append("  停入时间：2024-04-22 10:09:46\r\n");
            textBuff.append("  驶出时间：2024-04-22 10:29:59\r\n");
            textBuff.append("               \r\n");
            textBuff.append("  路段：龙昌路\r\n");
            textBuff.append("  车位号：035045\r\n");
            textBuff.append("  停车时长：46分钟 \r\n");
            textBuff.append("  停入时间：2024-04-19 08:24:41 \r\n");
            textBuff.append("  驶出时间：2024-04-19 09:10:10 \r\n");
            textBuff.append("               \r\n");
            textBuff.append("  路段：龙昌路 \r\n");
            textBuff.append("  车位号：035049 \r\n");
            textBuff.append("  应付金额：￥2.00 \r\n");
            textBuff.append("  停车时长：28分钟 \r\n");
            textBuff.append("  停入时间：2024-04-15 10:22:48 \r\n");
            textBuff.append("  驶出时间：2024-04-15 10:50:03 \r\n");
            textBuff.append("               \r\n");
            textBuff.append("  更多欠费订单信息，请扫码关注公众号查询\r\n");
            textBuff.append("               \r\n");
            textBuff.append("  车牌号：赣B79311 \r\n");
            textBuff.append("  欠费总金额：￥152.00 \r\n");
            text_label.setText(textBuff.toString());
        }


        //样板打印监听事件，功能暂未实现
        btn_templateprint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int dim = 25;
                int distance = 2;

                if(false == mContext.IsPrinterNormal() || mContext.get_is_printing())
                {
                    Toast.makeText(mContext, R.string.PrintBusy, Toast.LENGTH_SHORT).show();
                    return;
                }
                mContext.set_is_printing(true);

                mContext.mPrinter.initPrint();//清空缓存区

                try {
                    String StrTemp = template_chroma.getText().toString().trim();
                    dim = Integer.parseInt(StrTemp);
                } catch (Exception e) {
                    template_chroma.setText(dim + "");
                }
                mContext.mPrinter.setPrintChromaLevel(dim);

                try {
                    String StrTemp = template_Paper_distance.getText().toString().trim();
                    distance = Integer.parseInt(StrTemp);
                } catch (Exception e) {
                    template_Paper_distance.setText(distance + "");
                }

                printTemplate();


            }
        });

        btn_retteprint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  print = false;
            }
        });
    }

    private void printTemplate() {
        new Thread() {
            @Override
            public void run() {
                Log.d(TAG, "printTemplate..");

                //mContext.mPrinter.initPrint();//清空缓存区
                //mContext.mPrinter.setPrinterStatusCallBackEnable(false);
                if (mContext.mPrinter.blacklabel_status)
                { //标签测试
                    for(int i = 0; i < 3; i++)
                    {
                        String labeltest = text_label.getText().toString().trim();
                        StringBuilder labelBuff = new StringBuilder();
                        labelBuff.append("     这是一行测试标签  \r\n");
                        labeltest = labelBuff.toString();
                        //设置居中模式
                        mContext.mPrinter.setPrintAlignmentMode(1);
                        mContext.mPrinter.printText(labeltest);
                        Bitmap labelbitmap = CodeUtils.generateBitmap(labelbarcodeData, 120, 120);

                        try {
                            Thread.sleep(500);
                        } catch (Exception e) {
                        }
                        //打印条码
                        mContext.mPrinter.printImage(labelbitmap);

                        //跳转到下一张标签同样的位置
                        mContext.mPrinter.Feed_to_Next();
                    }
                }
                else
                {  //热敏样板测试
                  //  for (int i = 0; i < 6; i++) {
                        String testdata = text_label.getText().toString().trim();

                        int printstatus = mContext.GetPrinterStatus();
                        if(printstatus == 3)
                        {
                            Log.d(TAG, "printTemplate....缺纸 退出循环打印测试");
                            return;
                            //break;
                        }
                        StringBuilder textBuff = new StringBuilder();
                        textBuff.append("  停车欠费告知单  \r\n");
                        textBuff.append("               \r\n");
                        textBuff.append("  路段：龙南新世界路 \r\n");
                        textBuff.append("  车位号：034034\r\n");
                        textBuff.append("  应付金额：￥2.00\r\n");
                        textBuff.append("  停车时长：21分钟\r\n");
                        textBuff.append("  停入时间：2024-04-22 10:09:46\r\n");
                        textBuff.append("  驶出时间：2024-04-22 10:29:59\r\n");
                        textBuff.append("               \r\n");
                        textBuff.append("  路段：龙昌路\r\n");
                        textBuff.append("  车位号：035045\r\n");
                        textBuff.append("  停车时长：46分钟 \r\n");
                        textBuff.append("  停入时间：2024-04-19 08:24:41 \r\n");
                        textBuff.append("  驶出时间：2024-04-19 09:10:10 \r\n");
                        textBuff.append("               \r\n");
                        textBuff.append("  路段：龙昌路 \r\n");
                        textBuff.append("  车位号：035049 \r\n");
                        textBuff.append("  应付金额：￥2.00 \r\n");
                        textBuff.append("  停车时长：28分钟 \r\n");
                        textBuff.append("  停入时间：2024-04-15 10:22:48 \r\n");
                        textBuff.append("  驶出时间：2024-04-15 10:50:03 \r\n");
                        textBuff.append("               \r\n");
                        textBuff.append("  更多欠费订单信息，请扫码关注公众号查询\r\n");
                        textBuff.append("               \r\n");
                        textBuff.append("  车牌号：赣B79311 \r\n");
                        textBuff.append("  欠费总金额：￥152.00 \r\n");
                        textBuff.append("               \r\n");
                        testdata = textBuff.toString();
                        mContext.mPrinter.printText(testdata);
                        Log.d(TAG, "printTemplate111");
                        Bitmap bitmap = CodeUtils.generateBitmap(barcodeData, 320, 320);

                        try {
                            Thread.sleep(2000);
                        } catch (Exception e) {
                        }


                        //打印条码
                       // mContext.mPrinter.printImage(bitmap);
                        mContext.mPrinter.printImage3(bitmap,6);
                        String testdata2 = text_label.getText().toString().trim();
                        StringBuilder textBuff2 = new StringBuilder();
                        textBuff2.append("  温馨提醒：  \r\n");
                        textBuff2.append("  请扫描上方的二维码，可自助缴费\r\n");
                        textBuff2.append("               \r\n");
                        textBuff2.append("  欠费责任： \r\n");
                        textBuff2.append("  请您于即日起15天内补缴欠费，对逾\r\n");
                        textBuff2.append("  期未完成欠费补缴的单位、个人，将\r\n");
                        textBuff2.append("  通过公众号公布车牌号码，或会录入\r\n");
                        textBuff2.append("  社会诚信系统，且保留继续通过违约\r\n");
                        textBuff2.append("  金、法院起诉的权利进行追缴。请尽\r\n");
                        textBuff2.append("  快补缴费用。\r\n");
                        textBuff2.append("              \r\n");
                        textBuff2.append("  1小时以内按2元/次标准收取;\r\n");
                        textBuff2.append("  每增加1小时（不足1小时按1小时计\r\n");
                        textBuff2.append("  算）加收1元/小时;\r\n");
                        textBuff2.append("  收费时间段：8：00—21:00；\r\n");
                        textBuff2.append("  20分钟免费停车；\r\n");
                        textBuff2.append("  对新能源汽车停放服务收费实行优惠\r\n");
                        textBuff2.append("  政策，2小时内免费停放，按50%的收\r\n");
                        textBuff2.append("  费标准收取新能源汽车停放服务费。\r\n");
                        textBuff2.append("              \r\n");
                        textBuff2.append("  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n");

                        testdata2 = textBuff2.toString();
                        //Log.d(TAG, "printTemplate444 " + mContext.mPrinter.isPrinter + "GetPrinterStatus " + mContext.GetPrinterStatus());


                        mContext.mPrinter.printText(testdata2);

                        mContext.mPrinter.setFeedRow(0, 5);
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                        }
                   // }
            }
                mContext.set_is_printing(false);
            }
        }.start();
    }

}
